//页面加载完成执行里面的方法
$(function () {
    $("#page").load("../page.html");
    // findAll();
});
//对象的格式aa=｛key:value,key1:value1｝;aa.key,aa.key1
//data={result:1,list:array(35)} data.list
function findAll() {
    let data = {
        username: $("#username").val(),
        phone: $("#phone").val(),
        email: $("#email").val(),
        sex: $("#sex").val(),
        hobby: getHobby().toLocaleString(),
        startBirthday: $("#startBirthday").val(),
        endBirthday: $("#endBirthday").val(),
        // pageEntity: {
        //     curPage: $("#curPage").val(),
        //     pageSize: $("#pageSize").val()
        // },
        "pageEntity.curPage":$("#curPage").val(),
        "pageEntity.pageSize":$("#pageSize").val()
    };
    console.log(data);
    let result = myAjax("/back/user/findAll", data, "get");
    let count = myAjax("/back/user/getCount", data, "get");
    $("#countSize").html(count.countSize);
    setUserData(result);

}

function getHobby() {
    let vals = [];
    $('input:checkbox[name=hobby]:checked').each(function (index, item) {
        vals.push($(this).val());
    });
    return vals;
}

function setUserData(data) {
    let html = '';
    for (let i = 0; i < data.length; i++) {
        html += '<tr>\n' +
            '                                    <td>\n' +
            '                                      <input type="checkbox" name="id" value="1"   lay-skin="primary"> \n' +
            '                                    </td>\n' +
            '                                    <td>' + data[i].username + '</td>\n' +
            '                                    <td>' + getUndefined(data[i].email) + '</td>\n' +
            '                                    <td>' + getUndefined(data[i].phone) + '</td>\n' +
            '                                    <td>' + data[i].sex + '</td>\n' +
            '                                    <td>' + getUndefined(data[i].hobby) + '</td>\n' +
            '                                    <td>' + data[i].birthday + '</td>\n' +
            '                                    <td>' + (data[i].enable == 1 ? "正常" : "异常") + '</td>\n' +
            '                                    <td class="td-status">\n' +
            '                                      <span class="layui-btn layui-btn-normal layui-btn-mini">已启用</span></td>\n' +
            '                                    <td class="td-manage">\n' +
            '                                      <a onclick="member_stop(this,\'10001\')" href="javascript:;"  title="启用">\n' +
            '                                        <i class="layui-icon">&#xe601;</i>\n' +
            '                                      </a>\n' +
            '                                      <a title="编辑"  onclick="xadmin.open(\'编辑\',\'member-edit.html\',600,400)" href="javascript:;">\n' +
            '                                        <i class="layui-icon">&#xe642;</i>\n' +
            '                                      </a>\n' +
            '                                      <a onclick="xadmin.open(\'修改密码\',\'member-password.html\',600,400)" title="修改密码" href="javascript:;">\n' +
            '                                        <i class="layui-icon">&#xe631;</i>\n' +
            '                                      </a>\n' +
            '                                      <a title="删除" onclick="member_del(this,\'要删除的id\')" href="javascript:;">\n' +
            '                                        <i class="layui-icon">&#xe640;</i>\n' +
            '                                      </a>\n' +
            '                                    </td>\n' +
            '                                  </tr>';
    }
    $("#myBody").html(html);
}

function toEditPage(id) {
    //session  local
    localStorage.setItem("userId", id);
    sessionStorage.setItem("userId", id);
    openWindow("/html/back/user/userEdit.html");
}

function getUndefined(value) {
    return value == 'undefined' || value == 'null' || value == null ? '' : value;
}

function del(id) {
    $.ajax({
        url: '/back/user/del',
        data: {id: id},
        type: 'post',
        dataType: 'json',
        success: function (data) {
            console.log(data);
            if (data.result == 'success') {
                findAll();
            } else {
                alert("删除失败，请联系维护人员");
            }
        }
    });
}
